package com.inspur.ourhis_afterend.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inspur.ourhis_afterend.Dao.DeptDao;
import com.inspur.ourhis_afterend.Entity.Dept;
import com.inspur.ourhis_afterend.Service.DeptService;
import com.inspur.ourhis_afterend.Tools.PageAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    DeptDao deptDao;

    @Override
    public IPage<Dept> findAll(PageAction pageAction) throws Exception {
        IPage<Dept> iPage = new Page<>(pageAction.getPageNo(), pageAction.getPageSize());
        return deptDao.selectPage(iPage, null);
    }

    @Override
    public List<Dept> search(String departId, String name) {
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        if (departId != "") {
            queryWrapper.eq("departid", departId);
            System.out.println("查询id");
            return deptDao.selectList(queryWrapper);
        } else if (name != null) {
            queryWrapper.like("name", name);
            System.out.println("查询name");
            return deptDao.selectList(queryWrapper);
        }else {
            return null;
        }
    }

    @Override
    public void add(Dept dept) throws Exception {
        Dept dept1 = deptDao.selectById(dept.getDepartId());
        dept1.setName(dept.getName());
        System.out.println(dept1);
        deptDao.insert(dept1);
}

    @Override
    public int del(String departId) throws Exception {
        return deptDao.deleteById(departId);
    }

    @Override
    public int edit(Dept dept) throws Exception {
        return deptDao.updateById(dept);
    }
}
